package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.*;
import com.itheima.health.service.MenuStatusService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class MenuStatusServiceImpl implements MenuStatusService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        if (queryPageBean.getQueryString() != null) {
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("linkUrl", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("description", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<Menu> page = (Page<Menu>) menuMapper.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Menu findById(Integer id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        return menu;
    }

    @Override
    public void edit(Menu menu) {
        int i = menuMapper.updateByPrimaryKeySelective(menu);

    }

    @Override
    public void add(Menu menu) {
        menuMapper.insertSelective(menu);
    }

    @Override
    public List<Menu> findAll() {
        List<Menu> menuList = menuMapper.selectAll();
        return menuList;
    }

    @Override
    public List<Menu> getmenulist(String username) {
        //获取当前用户对应的role_id
        User userByName = userMapper.findUserByName(username);
        Integer userId = userByName.getId();
        Map<String, Integer> userRole = userRoleMapper.findUserRoleByUserid(userId);
        System.out.println("================");
        System.out.println(userRole);
        System.out.println("================");
        //Integer role_id = userRole.getRoleId();


        //获取role_id对应的menu_id
        List<Map<String, Integer>> menumaplist = roleMenuMapper.selectbyroleid(userRole.get("role_id"));

        List<Integer> menuidlist = new ArrayList<>();
        for (Map<String, Integer> map : menumaplist) {
            Integer menu_id = map.get("menu_id");
            menuidlist.add(menu_id);
        }


        //获取所有菜单
        List<Menu> menuList = new ArrayList<>();
        for (Integer integer : menuidlist) {
            Menu menu = menuMapper.findByMenuId(integer);
            menuList.add(menu);
        }


        //重组menu集合
        List<Menu> finalMenuList = new ArrayList<>();
        //获取第一级
        for (Menu menu : menuList) {
            if (menu.getParentMenuId() == null) {
                finalMenuList.add(menu);
            }
        }
        //获取第二级
        for (Menu menu : finalMenuList) {
            Integer parentId = menu.getId();
            for (Menu menu1 : menuList) {
                if (menu1.getParentMenuId() == parentId) {
                    menu.getChildren().add(menu1);
                }
            }
        }

        return finalMenuList;
    }
}
